/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gestionnaires;
import utilisateurs.modeles.Film;
import utilisateurs.modeles.LigneDePanier;
import utilisateurs.modeles.Panier;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
public class GestionnaireCommande extends AbstractFacade<LigneDePanier> {

    @PersistenceContext(unitName = "BlueRayPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public GestionnaireCommande() {
        super(LigneDePanier.class);
    }

    public LigneDePanier getByUserCart(Panier cart, Film film) {
        Query query = em.createQuery("SELECT lp FROM Panier p, IN(p.ligneDePanier) lp WHERE p.id = :panierId AND lp.jeuVideo.id = :jeuVideoId");
        query.setParameter("panierId", cart.getId());
        query.setParameter("jeuVideoId", film.getId());

        try {
            return (LigneDePanier) query.getSingleResult();
        } catch (NoResultException e) {
            return null;
        }
    }

    public void updateQuantity(LigneDePanier cartLine) {
        cartLine.setQuantite(cartLine.getQuantite() + 1);
        edit(cartLine);
    }

    public void updateQuantity(LigneDePanier cartLine, int quantity) {
        cartLine.setQuantite(quantity);
        edit(cartLine);
    }
}
